﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;
using BookStore.Common;

namespace BookStore.admin
{
    public partial class Customer : System.Web.UI.Page
    {
        static  bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll() {
            gvCustomer.DataSource = customerService.getAll();
            gvCustomer.DataBind();
        }
        public void ResetControl()
        {
            txtCustomerID.Text ="";
            txtCustomerName.Text ="";
            txtEmail.Text ="";
            txtPassword.Text ="";
            txtAddress.Text ="";
            txtPhone.Text ="";
        }
        public void insertCustomer() {
            customerInfo objCustomer = new customerInfo();
            objCustomer.customerName = txtCustomerName.Text;
            objCustomer.email = txtEmail.Text;
            objCustomer.password = encryption.toMD5(txtPassword.Text);
            objCustomer.address = txtAddress.Text;
            objCustomer.phone = txtPhone.Text;
            bool test = customerService.insertCustomer(objCustomer);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateCustomer()
        {
            customerInfo objCustomer = new customerInfo();
            objCustomer.customerID = Convert.ToInt32(txtCustomerID.Text);
            objCustomer.customerName = txtCustomerName.Text;
            objCustomer.email = txtEmail.Text;
            objCustomer.password = encryption.toMD5(txtPassword.Text);
            objCustomer.address = txtAddress.Text;
            objCustomer.phone = txtPhone.Text;
            bool test = customerService.insertCustomer(objCustomer);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
        protected void lbtBackT_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtAddT_Click(object sender, EventArgs e)
        {

            pnUpdate.Visible = true;
            ResetControl();
            flag = true;
            pnUpdate.Visible = true;
            pnView.Visible = false;

        }

        protected void lbtRefreshB_Click(object sender, EventArgs e)
        {

            showAll();
        }


        protected void lbtUpdateT_Click(object sender, EventArgs e)
        {
            if (flag==true)
            {
                insertCustomer();
            }
            else
            {
                updateCustomer();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

       

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                insertCustomer();
            }
            else
            {
                updateCustomer();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }


        protected void gvCustomer_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int customerID = Convert.ToInt32(gvCustomer.Rows[e.RowIndex].Cells[0].Text);
            bool test = customerService.deleteCustomer(customerID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();

        }

        protected void gvCustomer_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int customerID=Convert.ToInt32(gvCustomer.Rows[e.NewSelectedIndex].Cells[0].Text);
            customerInfo objCustomer = customerService.getbyID(customerID);
            txtCustomerID.Text = objCustomer.customerID.ToString();
            txtCustomerName.Text = objCustomer.customerName;
            txtEmail.Text = objCustomer.email;
            txtPassword.Text = objCustomer.password;
            txtAddress.Text = objCustomer.address;
            txtPhone.Text = objCustomer.phone;
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void gvCustomer_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCustomer.PageIndex = e.NewPageIndex;
            showAll();
        }


    }
}
